import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_syt/generated/l10n.dart';
import 'package:flutter_syt/theme/theme_color.dart';
import 'package:flutter_syt/utils/click_utils.dart';

class PickerItemEntity {
  late String name;
  late String placeholder;
  late Function updateData;

  PickerItemEntity(this.name, this.placeholder, this.updateData);
}

class AreaPicker extends StatefulWidget {
  final PickerItemEntity entity;
  const AreaPicker(this.entity, {Key? key}) : super(key: key);
  @override
  State<StatefulWidget> createState() => _AreaPickerState();
}

class _AreaPickerState extends State<AreaPicker> {
  final FocusNode focusNode = FocusNode();
  var formSetting;

  List provices = ['四川省'];
  List citys = [];
  List county = [];
  List pickerChildren = [];

  ///选中的省份的index
  int selectedProvice = 0;

  ///选中的市的index
  int selectedCity = 0;

  ///选中的区的index
  int selectedCounty = 0;
  String chooseValue = '';

  ///定义省份控制器
  late FixedExtentScrollController proviceCotroller;

  ///定义市控制器
  late FixedExtentScrollController cityController;

  ///定义区控制器
  late FixedExtentScrollController countyController;
  var address;
  @override
  void initState() {
    selectedProvice = 0;
    selectedCity = 0;
    selectedCounty = 0;
    proviceCotroller = new FixedExtentScrollController(initialItem: 0);
    cityController = new FixedExtentScrollController(initialItem: 0);
    countyController = new FixedExtentScrollController(initialItem: 0);
    _initAreaInfo();
    super.initState();
  }

  Future _initAreaInfo() async {
    String jsonString = await rootBundle.loadString("assets/area.json");
    pickerChildren = json.decode(jsonString);
    citys = pickerChildren[selectedProvice]['counties'];
    county = citys[selectedCity]['counties'];
  }

  void passParams() {
    setState(() {
      address = pickerChildren[selectedProvice]['name'] +
          citys[selectedCity]['name'] +
          county[selectedCounty]['name'];
      cityController =
          new FixedExtentScrollController(initialItem: selectedCity);
      countyController =
          new FixedExtentScrollController(initialItem: selectedCounty);
    });
    widget.entity.updateData(address);
  }

  @override
  Widget build(BuildContext context) {
    print(222);
    print(address);
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          widget.entity.name,
          style: TextStyle(
            color: ThemeColor.colorBlack60,
            fontSize: 16,
          ),
        ),
        GestureDetector(
          onTap: ClickUtils.debounce(() {
            showCupertinoModalPopup(
              context: context,
              builder: (BuildContext context) {
                return StatefulBuilder(
                  builder: (BuildContext context,
                      void Function(void Function()) setState) {
                    return Container(
                      height: 320,
                      decoration: BoxDecoration(
                          color: ThemeColor.bkColorFFFFFF,
                          borderRadius: BorderRadius.only(
                              topLeft: Radius.circular(16.0),
                              topRight: Radius.circular(16.0))),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.start,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Container(
                            height: 52,
                            padding: EdgeInsets.fromLTRB(16, 12, 16, 12),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                Text(
                                  widget.entity.name,
                                  style: TextStyle(
                                    fontSize: 16,
                                    color: Color(0xff111111),
                                    fontWeight: FontWeight.bold,
                                  ),
                                ),
                                Container(
                                  width: 80,
                                  height: 28,
                                  child: ElevatedButton(
                                    onPressed: ClickUtils.debounce(() {
                                      Navigator.pop(context);
                                      passParams();
                                    }),
                                    child: Text(
                                      S.of(context).confirm,
                                      style: TextStyle(
                                        fontSize: 14,
                                        color: Color(0xff111111),
                                        fontWeight: FontWeight.w400,
                                      ),
                                    ),
                                  ),
                                )
                              ],
                            ),
                          ),
                          Expanded(
                            child: Row(
                              mainAxisSize: MainAxisSize.min,
                              children: [
                                Expanded(
                                  flex: 1,
                                  child: CupertinoPicker(
                                    selectionOverlay: Container(
                                      decoration: BoxDecoration(
                                        border: Border.symmetric(
                                          horizontal: BorderSide(
                                              color: Color(0xffcccccc),
                                              width: 1.0),
                                        ),
                                      ),
                                    ),
                                    backgroundColor: ThemeColor.bkColorFFFFFF,
                                    diameterRatio: 1.1,
                                    scrollController: proviceCotroller,
                                    itemExtent: 48.0,
                                    onSelectedItemChanged: (position) {},
                                    children: provices.map((data) {
                                      return Center(
                                        child: Text(
                                          data,
                                          style: TextStyle(
                                            fontSize: 16,
                                            color: Color(0xff111111),
                                            fontWeight: FontWeight.bold,
                                          ),
                                        ),
                                      );
                                    }).toList(),
                                  ),
                                ),
                                Expanded(
                                  flex: 1,
                                  child: CupertinoPicker(
                                    selectionOverlay: Container(
                                      decoration: BoxDecoration(
                                        border: Border.symmetric(
                                          horizontal: BorderSide(
                                              color: Color(0xffcccccc),
                                              width: 1.0),
                                        ),
                                      ),
                                    ),
                                    backgroundColor: Colors.white,
                                    scrollController: cityController,
                                    itemExtent: 48.0,
                                    onSelectedItemChanged: (position) {
                                      setState(() {
                                        selectedCity = position;
                                        selectedCounty = 0;
                                        county = citys[position]['counties'];
                                      });
                                      countyController.jumpToItem(0);
                                    },
                                    children: citys.map((data) {
                                      return Center(
                                        child: Text(
                                          '${data['name']}',
                                          style: TextStyle(
                                            fontSize: 16,
                                            color: Color(0xff111111),
                                            fontWeight: FontWeight.bold,
                                          ),
                                        ),
                                      );
                                    }).toList(),
                                  ),
                                ),
                                Expanded(
                                  flex: 1,
                                  child: CupertinoPicker(
                                    selectionOverlay: Container(
                                      decoration: BoxDecoration(
                                        border: Border.symmetric(
                                          horizontal: BorderSide(
                                              color: Color(0xffcccccc),
                                              width: 1.0),
                                        ),
                                      ),
                                    ),
                                    backgroundColor: Colors.white,
                                    scrollController: countyController,
                                    itemExtent: 48.0,
                                    onSelectedItemChanged: (position) {
                                      selectedCounty = position;
                                    },
                                    children: county.map((data) {
                                      return Center(
                                        child: Text(
                                          '${data['name']}',
                                          style: TextStyle(
                                            fontSize: 16,
                                            color: Color(0xff111111),
                                            fontWeight: FontWeight.bold,
                                          ),
                                        ),
                                      );
                                    }).toList(),
                                  ),
                                ),
                              ],
                            ),
                          )
                        ],
                      ),
                    );
                  },
                );
              },
            );
          }),
          child: Container(
            padding: EdgeInsets.only(top: 6, bottom: 10),
            decoration: BoxDecoration(
              border: Border(
                bottom: BorderSide(width: 2, color: Color(0xffc6c6c6)),
              ),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  address != null ? address : widget.entity.placeholder,
                  style: TextStyle(
                    color: address != null
                        ? ThemeColor.colorBlack80
                        : ThemeColor.colorBlack30,
                    fontSize: 16,
                  ),
                ),
                Icon(
                  Icons.chevron_right,
                  color: ThemeColor.colorBlack60,
                ),
              ],
            ),
          ),
        ),
      ],
    );
  }
}
